package com.ncucoder.soms.util;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * 密码工具类
 *
 * @author <a href="https://www.ncucoder.com">hsowan</a>
 * @date 2019-07-10
 */
public class PasswordUtils {

    private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
    private static final String ALGORITHM_NAME = "md5";
    private static final int HASH_ITERATIONS = 2;

    public static String encrypt(String password, String salt) {
        return new SimpleHash(
                ALGORITHM_NAME,
                password,
                ByteSource.Util.bytes(salt),
                HASH_ITERATIONS).toHex();

    }

    public static String generateSalt() {
        return randomNumberGenerator.nextBytes().toHex();
    }

}
